package cn.cmkj.auction.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import cn.cmkj.auction.R;
import cn.cmkj.auction.data.http.Shuhua;
import cn.cmkj.auction.widget.ptr.recyclerview.BaseRecycleAdapter;

/**
 * Created by cunguoyao on 2017/9/24.
 */

public class GoodsOfOrderAdapter extends BaseRecycleAdapter {

    private Context context;
    private List<Shuhua> mData;
    private int fromPage;
    private ImageOptions imageOptions;

    public GoodsOfOrderAdapter(Context context, List<Shuhua> mData, int fromPage) {
        this.context = context;
        this.mData = mData;
        this.fromPage = fromPage;
        this.imageOptions = new ImageOptions.Builder().setFadeIn(true)//淡入效果
                .setFailureDrawableId(R.drawable.empty_photo)
                .setImageScaleType(ImageView.ScaleType.FIT_XY).build();
    }

    @Override
    public int onSpan(int position) {
        return 1;
    }

    @Override
    public int getItemCount() {
        int size = mData != null ? mData.size() : 0;
        return size;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext ()).inflate(R.layout.adapter_order_goods, parent, false);
        return new OrderViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Shuhua shuhua = mData.get(position);
        if(shuhua != null) {
            x.image().bind(((OrderViewHolder) holder).image, shuhua.getDefaultImage().m_url, imageOptions);
            ((OrderViewHolder) holder).textName.setText(shuhua.getName());
        }
        if(fromPage == 1) {
            ((OrderViewHolder) holder).textPrice.setText("￥" + shuhua.getSalePrice().doubleValue());
        }
        ((OrderViewHolder) holder).rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    class OrderViewHolder extends RecyclerView.ViewHolder {
        public RelativeLayout rootView;
        public ImageView image;
        public TextView textName;
        public TextView textPrice;

        public OrderViewHolder(View itemView) {
            super(itemView);
            this.rootView = (RelativeLayout) itemView.findViewById(R.id.relative_layout);
            this.image = (ImageView) itemView.findViewById (R.id.order_goods_image);
            this.textName = (TextView) itemView.findViewById (R.id.order_goods_name);
            this.textPrice = (TextView) itemView.findViewById (R.id.order_goods_price);
        }
    }
}
